capture log close
clear all
set maxvar 32767
set more off
pause off
# delimit;

local crime "";
local output "";


/* 
Summary stats for main (calendar time) dataset
*/


local exp "prworard";
local fs_vars  "age18_yes n_age18unfav n_onssi1922" ;
local hh_vars "n_earnhh9702 n_earnhh9712 n_emphhpost9702 n_emphhpost9712 n_inchh9702 n_inchh9712" ;
local covariates "n_male n_firstage n_fam_singmom n_fam_nopar n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_diag1_mental n_diag1_nerv n_diag1_sense n_diag1_infec n_diag1_endo n_diag1_none n_diag1_cong n_diag1_musc n_diag1_resp n_diag1_blood n_diag1_neo n_earnhhpre9095" ;
local main_vars "n_crime* c_crime* n_yr* n_1* n_2* n_3* n_4* avg*" ;

local demo_list1 "a o i p y t s m f w b h l x r" ; /* z n x r*/
local demo_list2 "f1 f2 f3 ch cl dh dl ph pl u7h u7l ugh ugl uah ual" ;
local num_1 "" ;
local num_2 "2" ;

local list = 1 ;

local bwidth = 264 ;
local bwidth_close = 90 ;

use "`crime'\prep2\crime_prwora_prepforreg.dta", clear ;


/* Create predicted removal probability */
xi: reg n_age18unfav 
	`controls_all' i.dibdig
	if n_post == 1 ;
predict hat_removal ;

summ hat_removal, det ;
gen byte demo_q25 		= (hat_removal <  r(p25)) if hat_removal != . ;
gen byte demo_q2575 	= (hat_removal >= r(p25) & hat_removal < r(p75)) if hat_removal != . ;
gen byte demo_q75 		= (hat_removal >= r(p75) & hat_removal != .) if hat_removal != . ;
gen byte demo_remlow 	= (hat_removal <  r(p50)) if hat_removal != . ;
gen byte demo_remhigh 	= (hat_removal >= r(p50) & hat_removal != .) if hat_removal != . ;

rename demo_remlow  demo_x ;
rename demo_remhigh demo_r ;


keep if abs(dobdd_run) <= `bwidth' ;

/* Subsamples */
gen demo_all = 1 ;
gen byte demo_o	= (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
gen byte demo_i = (n_diag1_mental == 1 & (substr(dibdig,1,3) == "317" | substr(dibdig,1,3) == "318" | substr(dibdig,1,3) == "319" )) ;
gen byte demo_p = (n_diag1_mental != 1) ;
gen byte demo_y = (n_diag1_mental == 1) ;
gen byte demo_t = (n_fam_singmom != 1 & n_fam_nopar != 1) ;
gen byte demo_s = (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
gen byte demo_m = (n_male ==1) ;
gen byte demo_f = (n_male !=1) ;
gen byte demo_w	= (race == "W") ;
gen byte demo_b = (race == "B") ;
gen byte demo_z = (race != "W") ;

gen byte demo_f1 	= (n_firstage <=6) ;
gen byte demo_f2 	= (n_firstage > 6 & n_firstage <=13) ;
gen byte demo_f3 	= (n_firstage > 13 & n_firstage < .) ;

egen cnty_u7 = rowmean(cnty_urate_1997-cnty_urate_1998) ;
egen cnty_ug = rowmean(cnty_urate_2007-cnty_urate_2012) ;
egen cnty_ua = rowmean(cnty_urate_1997-cnty_urate_2017) ;
rename cnty_crimerate cnty_c ;
rename cnty_share_blue cnty_d ;
rename cnty_pop2000 cnty_p ;

foreach cnty in c d p u7 ug ua { ;
	summ cnty_`cnty', det ;
	gen byte demo_`cnty'h = (cnty_`cnty' > r(p50) & cnty_`cnty' != .) ;
	gen byte demo_`cnty'l = (cnty_`cnty' <= r(p50) & cnty_`cnty' != .) ;
	replace demo_`cnty'h = . if cnty_`cnty' == . ;
	replace demo_`cnty'l = . if cnty_`cnty' == . ;
	tab demo_`cnty'h, m ;
	tab demo_`cnty'l, m ;
} ;

summ n_earnhhpre9095, det ;
replace n_earnhhpre9095 = 0 if n_earnhhpre9095 == . ;
gen byte demo_h = (n_earnhhpre9095 > r(p50) & n_earnhhpre9095 != .) ;
gen byte demo_l = (n_earnhhpre9095 <= r(p50) & n_earnhhpre9095 != .) ;
replace demo_h = . if n_earnhhpre9095 == . ;
replace demo_l = . if n_earnhhpre9095 == . ;
tab demo_h, m ;
tab demo_l, m ;

local summstats_lab "" ;
local tot = 0 ;

foreach demo in `demo_list`list'' { ;
	foreach var of varlist 
		`main_vars'
		{ ;
		local summstats_lab "`summstats_lab' `demo'`var' " ;
		local tot = `tot' + 1 ;
	} ;

			foreach var of varlist 
				`covariates' `fs_vars' `hh_vars' { ;	
				local summstats_lab "`summstats_lab' `demo'`var' " ;
				local tot = `tot' + 1 ;
	} ;
} ;	/* end demo */

matrix summstats=J(16,`tot',.) ;
local count = 0 ;

foreach demo in `demo_list`list'' { ;
	foreach var of varlist 
		`main_vars'
		{ ;
			
			/* Full sample */
			local count = `count' + 1 ;
			qui summ `var' if abs(dobdd_run) <= `bwidth' & demo_`demo'== 1, det ;
			matrix summstats[1,`count']=r(mean) ;
			matrix summstats[2,`count']=r(sd) ;
			matrix summstats[3,`count']=r(p50) ; 
			matrix summstats[4,`count']=r(N) ;
			
			/* Post Aug 22, 1996 (RHS, review) */
			qui summ `var' if n_post == 1 & abs(dobdd_run) <= `bwidth' & demo_`demo'== 1, det ;
			matrix summstats[5,`count']=r(mean) ;
			matrix summstats[6,`count']=r(sd) ;
			matrix summstats[7,`count']=r(p50) ; 
			matrix summstats[8,`count']=r(N) ;
			
			/* Before (LHS, no review) */
			qui summ `var' if n_post == 0 & abs(dobdd_run) <= `bwidth' & demo_`demo'== 1, det ;
			matrix summstats[9,`count']=r(mean) ;
			matrix summstats[10,`count']=r(sd) ;
			matrix summstats[11,`count']=r(p50) ; 
			matrix summstats[12,`count']=r(N) ;
			
			/* Before (LHS, no review) close to cutoff -- CONTROL MEANS FOR RD */
			qui summ `var' if n_post == 0 & dobmo_run >= -6 & dobmo_run < 0 & demo_`demo'== 1, det ;
			matrix summstats[13,`count']=r(mean) ;
			matrix summstats[14,`count']=r(sd) ;
			matrix summstats[15,`count']=r(p50) ; 
			matrix summstats[16,`count']=r(N) ;
			
	} ;

			foreach var of varlist 
				`covariates' `fs_vars' `hh_vars' { ;
					
				/* Full sample */
				local count = `count' + 1 ;
				qui summ `var' if abs(dobdd_run) <= `bwidth' & keep_1997_2017_2c == 1 & demo_`demo'== 1, det ;
				matrix summstats[1,`count']=r(mean) ;
				matrix summstats[2,`count']=r(sd) ;
				matrix summstats[3,`count']=r(p50) ; 
				matrix summstats[4,`count']=r(N) ;
				
				/* Post Aug 22, 1996 (RHS, review) */
				qui summ `var' if n_post == 1 & abs(dobdd_run) <= `bwidth' & keep_1997_2017_2c == 1 & demo_`demo'== 1, det ;
				matrix summstats[5,`count']=r(mean) ;
				matrix summstats[6,`count']=r(sd) ;
				matrix summstats[7,`count']=r(p50) ; 
				matrix summstats[8,`count']=r(N) ;
				
				/* Before (LHS, no review) */
				qui summ `var' if n_post == 0 & abs(dobdd_run) <= `bwidth' & keep_1997_2017_2c == 1 & demo_`demo'== 1, det ;
				matrix summstats[9,`count']=r(mean) ;
				matrix summstats[10,`count']=r(sd) ;
				matrix summstats[11,`count']=r(p50) ; 
				matrix summstats[12,`count']=r(N) ;
				
				/* Before (LHS, no review) close to cutoff -- CONTROL MEANS FOR RD */
				qui summ `var' if n_post == 0 & dobmo_run >= -6 & dobmo_run < 0 & keep_1997_2017_2c == 1 & demo_`demo'== 1, det ;
				matrix summstats[13,`count']=r(mean) ;
				matrix summstats[14,`count']=r(sd) ;
				matrix summstats[15,`count']=r(p50) ; 
				matrix summstats[16,`count']=r(N) ;
	} ;
} ; /* end demo */

clear ;
svmat summstats ; 
rename (summstats*) (`summstats_lab') ;
local samp1 "all" ;
local samp2 "postt" ;
local samp3 "prec" ;
local samp4 "prec_rd" ;
local stat1 "mean" ;
local stat2 "sd" ;
local stat3 "median" ;
local stat4 "N" ;
gen label = "" ;
forval i = 1(1)4 { ;
	forval j = 1(1)4 { ;
		replace label = "`samp`i''_`stat`j''" if _n == (`i' - 1)*4 + `j' ;
	} ;
} ;
order label ;
save "`output'\prwora_crime_summstats`num_`list''.dta", replace ;

capture log close;
